package com.moviemetagetter.ui.list;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JList;

public class FileList extends JList{

	public FileList(){
		super(new FileListModel());
	}
	
	public void loadFileList(String path){
		File dir = new File(path);
		List<File> files = new ArrayList<File>();
		visitAllDirsAndFiles(dir,files);
		
		setModel(new FileListModel(files));
	}
	
	private void visitAllDirsAndFiles(File dir,List<File> files) {
	    if(dir.isFile()){
	    	files.add(dir);
	    }

	    if (dir.isDirectory()) {
	        String[] children = dir.list(new FilenameFilter() {
				
				@Override
				public boolean accept(File dir, String name) {
					File file = new File(dir, name);
					if(file.isDirectory()){
						return true;
					}
					return (name.endsWith("m4v"));
				}
			});
	        
	        if(children == null){
	        	return;
	        }
	        for (int i=0; i<children.length; i++) {
	            visitAllDirsAndFiles(new File(dir, children[i]),files);
	        }
	    }
	}
	
	
}
